/*	ANKUR GOEL
	2010016	
	Group 1 , BTech 2010
	DSA Hashing assignment 	*/





#include<stdio.h>
#include<stdlib.h>
#include <string.h>




struct hashing
	{

		char user[20];
		char pass[20];
		

	}mytable[150];



void emptytable()
	{
		int i=0;
		while(i<150)
			{
				mytable[i].user[1]='\0';
				i++;
			}		
	}


int modhashingfunction(int k)
	{
		int i=0;
		i=k%150;
		
		return i;
	}		



void linearprobing(char name[] , char pwd[])
	{
		int i=0;
		int ascii=0 , temp=0,flag=0;
		
		while(name[i]!='\0')
			{
				temp=name[i];
				ascii=ascii+temp;
				i++;
			}
		i=0;

		int hkey=modhashingfunction(ascii);

		while((flag!=1)&&(i<150))
			{
				if(mytable[hkey].user[1]=='\0')
					{
						strcpy(mytable[hkey].user, name);	
						strcpy(mytable[hkey].pass, pwd);
						flag=1;	
					}
				else
					{
						hkey=(hkey+1)%150;
						i++;
					}
			}
		//printf("\n The total ascii value of %s is %d " , name , ascii );			

	}





void display()
	{
		int i=0;
			
		while(i<150)
			{
				if(mytable[i].user[1]!='\0')
					{
						printf("\n The key or username at position %d is %s \n",i,mytable[i].user);	
						printf("\n The password for it is %s \n",mytable[i].pass);
					}
				i++;
			}
	}


int search(char name[],char pass[],int i)
	{
		
		int ascii=0 , temp=0,j=0,hkey=0,flag=0,k=0;
		
		while(name[j]!='\0')
			{
				temp=name[j];
				ascii=ascii+temp;
				j++;
			}
		j=0;
		hkey=modhashingfunction(ascii);		
		 
		while((mytable[hkey].user[1]!='\0')&&(j<150))
			{	
				k=0;
				while(((mytable[hkey].user[k]!='\0')||(name[k]!='\0'))&&(k<20))
								{
									if(mytable[hkey].user[k]!=name[k])
										{
											flag=1;
											break;
										}
									k++;
								}
				
				k=0;
				while((((mytable[hkey].pass[k]!='\0')||(pass[k]!='\0'))&&(k<20))&&(flag==0))
								{
									if(mytable[hkey].pass[k]!=pass[k])
										{
											flag=1;
											break;
										}
									k++;
								}


				if((flag!=1))
					{	
						i=0;
						break;
					}
				else
					{
						hkey=(hkey+1)%150;
						j++;
					}
			}			

		return i;

	}

void usermenu()
	{	
		int i=1;
		int f=0;
		while(i<4)
			{
				char name[20] , pass[20],ch=' ';
				int k=0;
				ch=getchar();
				printf("\n\n		LOGIN PAGE !!! \n ");
				printf("\n	Please enter your username : ");

			
	
					ch=getchar();					
					while(ch!='\n')
					{	
						name[k]=ch;
						k++;
						ch=getchar();
					}
					name[k]='\0';
				

				
				k=0;	
		

				printf("\n	Please enter your password : ");
				ch=getchar();
				while(ch!='\n')
					{	pass[k]=ch;
						k++;
						ch=getchar();
					}
				pass[k]='\0';	
				i=search(name , pass , i);
				
				if(i==0)
					{
						printf("\n	SUCCESS !! The username and password have been accepted.	\n");
						break;
					}

				else if(i==3)
					{
						printf("\n	Username or password is incorrect !!!");
						printf("\n	You have reached the attempt limit , please re-load program to try again");
						i++;
					}

				else
					{
						printf("\n	Username or password is incorrect !!!  Please enter again. ");
						printf("\n	Attempts remaining = %d \n" , 3-i);	
						i++;
					}
			}			
	
	}


void readfile()
	{	emptytable();
		char filename[30];
		char ch , name[150][20] , pwd[150][20];
		int i=0 , lineflag=0 , spaceflag=0;
		int j=0;
		printf("Enter name of the source file :");
		scanf("%30s", filename);

		FILE *f;
		
		f=fopen(filename, "r");
		if(f==NULL)
			printf("\n The file entered couldnt be opened !!!\n");
		else
			{	ch=getc(f);
				while(ch!=EOF)
					{
						
						if(spaceflag==0)
							{
								if(ch!=' ')
									{	
										name[j][i]=ch;
										i++;
								
										
									}

								else
									{						
										name[j][i]='\0';
										spaceflag=1;
										i=0;
									}
							}
						
						else
							{
								if(ch!='\n')
									{	
										pwd[j][i]=ch;
										i++;
										
									}
								else
									{
										pwd[j][i]='\0';
										i=0;
										spaceflag=0;
										lineflag=1;
									}
							
								if(lineflag==1)
									{	//strcpy(mytable[j].user, name[j]);	
										//strcpy(mytable[j].pass, pwd[j]);	
										linearprobing( name[j] , pwd[j]);
										j++;
										lineflag=0;
										
							//call the hashing function and save name n pwd in username n passd of hashing value
									}
							}					
						ch=getc(f);
						
					}	
				//display();
				usermenu();
			}

	}
			
void main()
	{	
		readfile();	
		
	
		//int i=0;
		//while(i<5)
		//	{
		//		printf("%s" , mytable[i].user );
		//		printf("%s" , mytable[i].pass );
		//		i++;
		//	}

	}		




/*		do
		 {      
			printf("\n\n Enter element number %d of your new list : " , count+1);
			scanf("%d" , (a+count));
			count++;
			showlist(a , n);
			printf("\n Do you want to enter more elements (1--> yes , 2--> no ) : ");
			scanf("%d" , &yesno);	
			
			if(yesno==1)
				{	n++;
					a=realloc(a,n*sizeof(int)); 	
				}			
						

			while((yesno!=1)&&(yesno!=2))
				{
					printf("\n\n You entered an incorrect choice , please enter again !! \n");
					printf("\n Do you want to enter more elements (1--> yes , 2--> no ): ");
					scanf("%d" , &yesno);		
					if(yesno==1)
						{	n++;
							a=realloc(a,n*sizeof(int)); 	
						}		
				}		

		 }while(yesno == 1);
		 showlist(a , n);
		 size=n;
		 return a;
		
*/
     
			
